import { observable,action } from 'mobx';
import { ListPage } from 'weaver-mobile-page';
import { Modal,Toast,Tools } from 'weaver-mobile';
import * as API from '../apis/forwardback';
const getLabel = Tools.getLabel;

/**
 * @author jh
 * @description 转发收回
 */
export class ForwardBackStore {

    @observable selectRow = [];
    @observable listStore = new ListPage.Store();
    @observable listDatas = [];
    @observable template = {
        default: [
            {
                key: 'col1',
                configs: [
                    {
                        key: 'col1_row1',
                        configs: [
                            { key: 'operatorNames' },
                        ],
                    },
                    {
                        key: 'col1_row2',
                        configs: [
                            { key: 'operateTime' },
                        ],
                    },
                    {
                        key: 'col1_row3',
                        configs: [
                            { key: 'receiverNames' },
                        ],
                    },
                ],
            },
        ],
    };
    constructor() {
        this.getLogDatas = this.getLogDatas.bind(this);
        this.doForwardBack = this.doForwardBack.bind(this);
        this.setSelectDatas = this.setSelectDatas.bind(this);
        this.setCallBack = this.setCallBack.bind(this);
    }

    /**
     * 获取转发记录
     */
    @action
    getLogDatas(para ={}){

        API.getLogDatas(para).then(action((reVal)=>{
            let data = reVal.tableDatas;
            data.map((o)=>{
                const names = o.receiverNames.join(',');
                o.receiverNames = getLabel('15525','接收人：')+"：" + names;
                o.operateTime = getLabel('390524','转发时间')+"：" + o.operateTime;
            });
            this.listStore.setState({data});
            this.listDatas = data;
        }));
    }

    @action
    setSelectDatas(ids = []){
        this.selectRow = ids;
    }

    @action
    setCallBack(callback){
        this.callback = callback;
    }

    @action
    doForwardBack(){

        if(this.selectRow.length == 0 ) return;
        const params ={selectRow:this.selectRow};
        Modal.alert(getLabel('558','提示'), getLabel('500184','确认收回已选中转发记录？'), [{
                text: getLabel('31129','取消'),
                onPress: null
            }, {
                text: getLabel('826','确定'),
                onPress: () =>{
                    API.doForwardBack(params).then(action((reVal)=>{
                        if(reVal.success){
                            Toast.success(reVal.msg,2,()=>{
                                this.callback(reVal)
                            });
                        }else{
                            Toast.fail(getLabel('388077','收回转发失败！'));
                        }
                    }));
                } },
        ]);
    }

}